package cn.edu.scau.cmi.chenyiqiu.spring.hibernate.dao;


import org.hibernate.Session;
import org.hibernate.Transaction;

import cn.edu.scau.cmi.chenyiqiu.spring.domain.Teacher;
import cn.edu.scau.cmi.chenyiqiu.comprehension.util.HibernateFactory;


public class TeacherDao{
	
	private Session session;
	
	private Session getSession() {
		session=HibernateFactory.getSession();
		return session;
	}
	
	public void save(Teacher teacher) {
		getSession();
		Transaction transaction=session.beginTransaction();
		session.save(teacher);
		transaction.commit();
	}
	
	public void update(Teacher teacher) {
		getSession();
		Transaction transaction=session.beginTransaction();
		session.merge(teacher);
		transaction.commit();
	}
	
	public void delete(Teacher teacher) {
		getSession();
		Transaction transaction=session.beginTransaction();
		session.delete(teacher);
		transaction.commit();
	}
	
	public Teacher findById(Long id) {
		Teacher teacher=(Teacher) getSession().get(Teacher.class,id);
		return teacher;
	}
	
}
